package com.alliky.main.ui.activity;

import android.os.Bundle;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.alliky.core.base.AppManager;
import com.alliky.core.base.BaseActivity;
import com.alliky.core.base.BaseViewModel;
import com.alliky.core.net.RxRetrofit;
import com.alliky.core.router.RoutePath;
import com.alliky.core.util.RxToast;
import com.alliky.main.BR;
import com.alliky.main.R;
import com.alliky.main.databinding.ActivityMainBinding;

import java.util.ArrayList;
import java.util.List;

import me.majiajie.pagerbottomtabstrip.NavigationController;
import me.majiajie.pagerbottomtabstrip.listener.OnTabItemSelectedListener;

@Route(path = RoutePath.Main.ACTIVITY_MAIN)
public class MainActivity extends BaseActivity<ActivityMainBinding, BaseViewModel> {

    private List<Fragment> mFragments;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_main;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        initFragment();
        initBottomTab();
    }

    private void initFragment() {
        //ARouter拿到多Fragment(这里需要通过ARouter获取，不能直接new,因为在组件独立运行时，宿主app是没有依赖其他组件，所以new不到其他组件的Fragment)
        Fragment homeFragment = (Fragment) ARouter.getInstance().build(RoutePath.Home.FRAGMENT_HOME).navigation();
        Fragment workFragment = (Fragment) ARouter.getInstance().build(RoutePath.Classify.FRAGMENT_CLASSIFY).navigation();
        Fragment msgFragment = (Fragment) ARouter.getInstance().build(RoutePath.Cart.FRAGMENT_CART).navigation();
        Fragment meFragment = (Fragment) ARouter.getInstance().build(RoutePath.User.FRAGMENT_MY).navigation();
        mFragments = new ArrayList<>();
        mFragments.add(homeFragment);
        mFragments.add(workFragment);
        mFragments.add(msgFragment);
        mFragments.add(meFragment);
        if (homeFragment != null) {
            //默认选中第一个
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.add(R.id.frameLayout, homeFragment);
            transaction.commitAllowingStateLoss();
        }
    }

    private void initBottomTab() {
        NavigationController navigationController = binding.pagerBottomTab.material()
                .addItem(R.mipmap.icon_tab_home, "首页", ContextCompat.getColor(this, R.color.blue))
                .addItem(R.mipmap.icon_tab_classify, "分类", ContextCompat.getColor(this, R.color.blue))
                .addItem(R.mipmap.icon_tab_cart, "购物车", ContextCompat.getColor(this, R.color.blue))
                .addItem(R.mipmap.icon_tab_my, "我的", ContextCompat.getColor(this, R.color.blue))
                .setDefaultColor(ContextCompat.getColor(this, R.color.textColorVice))
                .build();
        //底部按钮的点击事件监听
        navigationController.addTabItemSelectedListener(new OnTabItemSelectedListener() {
            @Override
            public void onSelected(int index, int old) {
                Fragment currentFragment = mFragments.get(index);
                if (currentFragment != null) {
                    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                    transaction.replace(R.id.frameLayout, currentFragment);
                    transaction.commitAllowingStateLoss();
                }
            }

            @Override
            public void onRepeat(int index) {
            }
        });
    }

    /**
     * 监听Back键按下事件,方法2:
     * 注意:
     * 返回值表示:是否能完全处理该事件
     * 在此处返回false,所以会继续传播该事件.
     * 在具体项目中此处的返回值视情况而定.
     */
    private static long lastTime;

    @Override
    public void onBackPressed() {
        long currentTime = System.currentTimeMillis();
        if (currentTime - lastTime > 2000) { // 如果两次按键时间间隔大于2秒，则不退出
            lastTime = currentTime;// 更新lastTime
            RxToast.normal("再按一次退出程序");
        } else {
            // 两次按键小于2秒时，退出应用
            AppManager.getInstance().AppExit();
        }
    }
}
